﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Net.WebRequestMethods;

namespace PushFormsApp
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        public event FileSystemEventHandler Deleted = null;
        public event FileSystemEventHandler CreatedDolder = null;
        public event FileSystemEventHandler CreatedFile = null;
        public event FileSystemEventHandler Changed = null;
        public event RenamedEventHandler Renamed = null;
        string JobName = "";
        string filePath = @"D:\fileA";
        Dictionary<string, string> dicFile = new Dictionary<string, string>();
        private void button1_Click(object sender, EventArgs e)
        {
            SartFileWatcher(this.filePath);
        }

        public void SartFileWatcher(string filePath)
        {
            string filter = "";
            //监听整个目录

            FileWatcher watcher = string.IsNullOrEmpty(filter) ? new FileWatcher(filePath) : new FileWatcher(filePath, filter);
            watcher.Changed += Watcher_Changed;
            watcher.CreatedDolder += Watcher_CreatedDolder;
            watcher.CreatedFile += Watcher_CreatedFile;
            watcher.Deleted += Watcher_Deleted;
            watcher.Renamed += Watcher_Renamed;

            watcher.Open();

            while (true)
            {
                if (watcher.IsWatch)
                {

                }
                else
                {
                    watcher.Open();
                }
                System.Threading.Thread.Sleep(10000);
            }

        }
        private void Watcher_Renamed(object sender, System.IO.RenamedEventArgs e)
        {
            Console.WriteLine($"Watcher_Renamed:({JobName})" + e.FullPath);

            Renamed?.Invoke(sender, e);
        }

        /// <summary>
        /// 监控删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Watcher_Deleted(object sender, System.IO.FileSystemEventArgs e)
        {
            Console.WriteLine($"Watcher_Deleted:({JobName})" + e.FullPath);
            try
            {
                string targetPath = e.FullPath.Replace("D:", "E:");
                string fileName = e.Name;
                if (FileHelper.IsExistFile(targetPath))
                {
                    FileHelper.DeleteFile(targetPath);
                    Console.WriteLine("Deleted:" + targetPath + DateTime.Now.ToString());
                    System.Threading.Thread.Sleep(2000);
                }

            }
            catch (Exception ex)
            { }

            Deleted?.Invoke(sender, e);
        }

        /// <summary>
        /// 监控有新建文件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Watcher_CreatedFile(object sender, System.IO.FileSystemEventArgs e)
        {
            Console.WriteLine($"Watcher_CreatedFile:({JobName})" + e.FullPath);

            try
            {

                if (Directory.Exists(filePath))
                {
                    System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(filePath);
                    System.IO.FileInfo[] allFiles = dir.GetFiles(".", System.IO.SearchOption.AllDirectories);
                    foreach (System.IO.FileInfo file in allFiles)
                    {
                        string filename = file.FullName;
                        string extension = Path.GetExtension(filename);
                        string md5 = FileHelper.GetMD5Key(filename);
                        if (dicFile.ContainsKey(filename) && dicFile[filename] != md5)
                        {
                            dicFile[filename] = md5;
                        }
                        if (!dicFile.ContainsKey(filename))
                            dicFile.Add(filename, md5);
                        Console.WriteLine("Created:" + filename + "  md5:" + md5 + " date:" + DateTime.Now.ToString());
                        FileHelper.Copy(filename, filename.Replace("D:", "E:"));
                        System.Threading.Thread.Sleep(2000);
                    }

                }

            }
            catch (Exception ex)
            { }

            CreatedFile?.Invoke(sender, e);
        }

        private void Watcher_CreatedDolder(object sender, System.IO.FileSystemEventArgs e)
        {
            Console.WriteLine($"Watcher_CreatedDolder:({JobName})" + e.FullPath);

            CreatedDolder?.Invoke(sender, e);
        }

        private void Watcher_Changed(object sender, System.IO.FileSystemEventArgs e)
        {
            try
            {

                if (Directory.Exists(filePath))
                {
                    System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(filePath);
                    System.IO.FileInfo[] allFiles = dir.GetFiles(".", System.IO.SearchOption.AllDirectories);
                    foreach (System.IO.FileInfo file in allFiles)
                    {
                        string filename = file.FullName;
                        string extension = Path.GetExtension(filename);
                        string md5 = FileHelper.GetMD5Key(filename);
                        if (dicFile.ContainsKey(filename) && dicFile[filename] == md5)
                        {
                            continue;
                        }
                        if (dicFile.ContainsKey(filename) && dicFile[filename] != md5)
                        {
                            dicFile[filename] = md5;
                        }
                        string targetFilename = filename.Replace("D:", "E:");
                        FileHelper.DeleteFile(targetFilename); 
                        FileHelper.Copy(filename, targetFilename);
                        Console.WriteLine("Changed " + filename + "  md5:" + md5 + " date:" + DateTime.Now.ToString());

                        System.Threading.Thread.Sleep(4000);
                    }

                }

            }
            catch (Exception ex)
            { }
            Changed?.Invoke(sender, e);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            FileWatcher watcher = string.IsNullOrEmpty(filePath) ? new FileWatcher(filePath) : new FileWatcher(filePath, filePath);
            watcher.Changed += Watcher_Changed;
            watcher.CreatedDolder += Watcher_CreatedDolder;
            watcher.CreatedFile += Watcher_CreatedFile;
            watcher.Deleted += Watcher_Deleted;
            watcher.Renamed += Watcher_Renamed;

            watcher.Close();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            SartFileWatcher(this.filePath);
        }
    }


}
